package com.zhaowei.codingintervierguide.stackandqueue;

import java.util.Stack;

class MyStack {
    private Stack<Integer> stackDate;
    private Stack<Integer> minDate;

    public MyStack() {
        this.stackDate = new Stack<Integer>();
        this.minDate = new Stack<Integer>();
    }

    public void push(Integer element) {
        if (this.minDate.empty()) {
            this.minDate.push(element);
        } else if (element.compareTo(this.minDate.peek()) < 0) {
            this.minDate.push(element);
        }
        this.stackDate.push(element);
    }

    public Integer pop() {
        if (this.stackDate.empty()) {
            return null;
        } else {
            Integer value = this.stackDate.pop();
            if (value.compareTo(this.getMin()) == 0) {
                this.minDate.pop();
            }
            return this.stackDate.pop();
        }
    }

    public Integer getMin() {
        return this.minDate.peek();
    }
}

public class GetMinStack {

    public static void main(String[] args) {
        MyStack myStack = new MyStack();
        myStack.push(2);
        myStack.push(4);
        System.out.println(myStack.getMin());
        myStack.push(5);
        myStack.push(1);
        System.out.println(myStack.getMin());
        myStack.pop();
        System.out.println(myStack.getMin());

    }
}
